package com.huawei.statistics.utils;

import org.apache.poi.ss.usermodel.*;

import java.io.File;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;

/**
 * Created by Yinwx on 2019/7/22.
 */
public class ExcelUtil {

    /**
     * 读取Excel测试，兼容 Excel 2003/2007/2010
     */
    public static String readExcel()
    {
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        StringBuffer str = new StringBuffer("");
        try {
            //同时支持Excel 2003、2007
            File excelFile = new File("F:\\钉钉打卡考勤地址-IMC-广东-尹文祥.xls"); //创建文件对象
            FileInputStream is = new FileInputStream(excelFile); //文件流
            //这种方式 Excel 2003/2007/2010 都是可以处理的
            Workbook workbook = WorkbookFactory.create(is);
            int sheetCount = workbook.getNumberOfSheets();  //Sheet的数量
            //遍历每个Sheet

            for (int s = 0; s < sheetCount; s++) {
                Sheet sheet = workbook.getSheetAt(s);
                int rowCount = sheet.getPhysicalNumberOfRows(); //获取总行数
                //遍历每一行
                for (int r = 0; r < rowCount; r++) {
                    Row row = sheet.getRow(r);
                    int cellCount = row.getPhysicalNumberOfCells(); //获取总列数
                    //遍历每一个单元格
                    for (int c = 0; c < cellCount; c++) {
                        Cell cell = row.getCell(c);
                        int cellType = cell.getCellType();
                        String cellValue = null;

                        //在读取单元格内容前,设置所有单元格中内容都是字符串类型
                        cell.setCellType(Cell.CELL_TYPE_STRING);

                        //按照字符串类型读取单元格内数据
                        cellValue = cell.getStringCellValue();

                    /*在这里可以对每个单元格中的值进行二次操作转化*/
                        str.append(cellValue+ "        ");
                        System.out.print((!StringUtil.isNotEmpty(cellValue)?"           ":cellValue) + "        ");
                    }
                    System.out.println();
                }
            }

        }
        catch (Exception e) {
            e.printStackTrace();
        }

        return str.toString();
    }


    public static void main(String[] args) {

        String str = readExcel();
        System.out.println(str);
    }


}
